Skip to main content

Master-Master Architecture

Master-Master replication, also known as Peer-to-Peer replication, is a system design pattern where multiple nodes (or instances) are all capable of handling reads and writes. Each node is considered a master, and they synchronize data between each other to stay consistent.

Unlike master-slave, where only one node handles writes, in master-master all nodes can perform write operations.

Characteristics of Master-Master

CharacteristicDescription
Write at any nodeAny master node can handle client write requests
Bi-directional syncAll nodes replicate data to each other to stay in sync
Conflict resolutionNeeded when the same data is modified at multiple nodes simultaneously
High availabilityNo single point of failure; all nodes are active
Eventual consistencyEnsures all replicas eventually converge to the same state

How it work

  1. Client sends write request to any master node.
  2. The node updates its local copy and then propagates the change to other master nodes.
  3. Other nodes apply the update, resolving any conflicts if needed.
  4. All nodes now reflect the same final state (eventually consistent).

Use Cases of Master-Master

Use CaseReason for Master-Master
Globally distributed databasesHandle local writes without latency from cross-region calls
High availability appsPrevent downtime if one node fails
Offline-first or mobile sync appsAllow clients to write offline, sync later with conflict resolution

Technologies Supporting Master-Master

TechnologyDescription
CouchDBBuilt-in multi-master with sync protocols
CassandraPeer-to-peer with eventual consistency
MySQL (with Galera)Provides multi-master synchronous replication
Redis EnterpriseActive-active geo-distributed Redis
Firebase Realtime DBSupports syncing across clients with conflict resolution

Conflict Resolution

When two masters write to the same key at the same time, a conflict arises.

Solutions:

  • Last Write Wins (LWW): Use timestamps to keep the latest update.
  • Application-level merge logic: E.g., merging two versions of a document.
  • Operational Transformation (OT) or CRDTs: Used in collaborative editing apps like Google Docs.

Example of Master-Master

Scenario

A company runs a shopping app with users in Europe and Asia.

  • Data is stored in two MySQL nodes:
    • EU_DB
    • ASIA_DB
  • Both are masters with Galera Cluster for synchronous replication.

Flow

  1. A user in Europe updates their shipping address → write goes to EU_DB.
  2. Galera replicates this update to ASIA_DB.
  3. A user in Asia updates their phone number → write goes to ASIA_DB.
  4. The change is replicated back to EU_DB.

All changes are synchronized in real-time across both regions.

Diagram

                   +-----------+
| Client 1 |
+-----------+
|
+----------+
| Master A |◄─────────────────────────┐
+----------+ │
▲ │
│ bi-directional replication │
▼ │
+----------+ │
| Master B |──────────────────────────┘
+----------+
|
+-----------+
| Client 2 |
+-----------+

Pros and Cons of Master-Master

✅ Pros❌ Cons
High availability (no single point of failure)Conflict resolution complexity
Scalable for global appsHarder to guarantee strict consistency
Writes are locally fast (geo-distributed)Potential for write-write conflicts
Load can be evenly distributedMore complex deployment and monitoring

Master-Slave vs Master-Master

FeatureMaster-SlaveMaster-Master
Write node(s)Only masterAny node (all are writable)
Conflict possibilityNo (single writer)Yes (must be resolved)
AvailabilityLower (if master fails)Higher (no single point of failure)
PerformanceRead-heavy workloadsRead/write-heavy & globally distributed
ComplexityLowerHigher